package cc.mrbird.plan.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.plan.domain.Training;
import cc.mrbird.plan.service.TrainingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(description = "演练信息管理接口")
@Controller
public class TrainingController extends BaseController{
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TrainingService trainingService;

    @ApiOperation(value = "演练信息添加")
    @RequestMapping(value = "training/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(Training training) {
        try {
            Training result = this.trainingService.findById(training.getUnit_id());
            if (result != null) {
                return ResponseBo.warn("该演练信息已存在！");
            }
            this.trainingService.add(training);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
    @ApiOperation(value = "根据id查询演练信息")
    @Log("根据id查询演练信息")
    /*@RequiresPermissions("training:findById")*/
    @RequestMapping(value = "training/findById",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo findById(Integer id) {
        try {
            Training training = this.trainingService.findById(id);
            return ResponseBo.ok(training);
        } catch (Exception e) {
            log.error("根据id查询演练信息失败", e);
            return ResponseBo.error("根据id查询演练信息失败，请联系网站管理员！");
        }
    }
    @ApiOperation(value = "修改演练信息信息")
    @Log("修改演练信息信息")
    /* @RequiresPermissions("training:update")*/
    @RequestMapping(value = "training/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(Training training) {
        try {
            this.trainingService.update(training);
            return ResponseBo.ok("修改演练信息信息成功！");
        } catch (Exception e) {
            log.error("修改演练信息信息失败", e);
            return ResponseBo.error("修改演练信息失败，请联系网站管理员！");
        }
    }
    @Log("删除演练信息")
    /*@RequiresPermissions("training:delete")*/
    @RequestMapping(value = "training/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.trainingService.delete(ids);
            return ResponseBo.ok("删除演练信息成功！");
        } catch (Exception e) {
            log.error("删除演练信息失败", e);
            return ResponseBo.error("删除演练信息失败，请联系网站管理员！");
        }
    }
}
